/**
 * Created by zycool on 2016/9/6.
 * 详情 - 运行计划 - 阴保运行管理 - 腐蚀控制
 */


;(function($,window,document,undefined){

    // 设置日期插件为中文
    $.datetimepicker.setLocale('zh');

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title:'管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId':$pipeline,'pipeSegmentId':$segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title:'终点位置',
        server: true,
        param: {'pipeLineId':$pipeline,'pipeSegmentId':$segment,'beginSignId':$begin}
    });
    // 触发管道
    $pipeline.element.on('select',function(e,v){
        var id = v || $pipeline.val();
        if(id){
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId:id}, function(dataset){
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId:id}, function(dataset){
                var begin = DyyBase.getPos(dataset,'begin');
                var end = DyyBase.getPos(dataset,'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        }else{
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select',function(e,v){
        var id = v || $segment.val();
        if(id){
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId:id}, function(dataset){
                var begin = DyyBase.getPos(dataset,'begin');
                var end = DyyBase.getPos(dataset,'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        }else{
            $begin.add([]);
            $end.add([]);
        }
    });
    // 开始时间
    var $date_begin = $('#JQ_date_begin');
    $date_begin.datetimepicker({
        timepicker: false,
        format: 'Y-m-d',
        scrollInput: false,
        style: 'z-index:102'
    });
    // 结束时间
    var $date_end_input = $('#JQ_date_end_input');
    $date_end_input.datetimepicker({
        timepicker: false,
        format: 'Y-m-d',
        scrollInput: false,
        minDate: new Date(),
        style: 'z-index:102'
    });
    // 天 - 周 - 月
    var $day_week_month_select = $('#JQ_day_week_month_select').select3({
        allowClear: false,
        search: false,
        data: [
            {id:'DAY',text:'天'},
            {id:'WEEK',text:'周'},
            {id:'MONTH',text:'月'}
        ]
    });
    // 标准条例表格
    var $plan_tbody = $('#JQ_plan_tbody');

    // 标准条例模板
    var planList = [
        {device:'长效参比',parameter:'长效饱和硫酸铜参比电极校准',gbt19285:'',gbt21448:'',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08103#1HHHHH704BF1749EE0530E0AA8C08007'},
        {device:'长效参比',parameter:'高纯锌参比电极',gbt19285:'',gbt21448:'',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08103#1HHHHH704BF1749EE0530E0AA8C08008'},
        {device:'测试桩',parameter:'自然电位',gbt19285:'',gbt21448:'',syt5919:'',cjj95:'1月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08104#1HHHHH704BF1749EE0530E0AA8C08009'},
        {device:'测试桩',parameter:'通电电位',gbt19285:'1月/次',gbt21448:'',syt5919:'',cjj95:'1月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08104#1HHHHH704BF1749EE0530E0AA8C08010'},
        {device:'测试桩',parameter:'断电电位',gbt19285:'',gbt21448:'1年/次',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08104#1HHHHH704BF1749EE0530E0AA8C08011'},
        {device:'测试桩',parameter:'测试桩维护',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08104#1HHHHH704BF1749EE0530E0AA8C08012'},
        {device:'跨接线',parameter:'管/地电位测试',gbt19285:'',gbt21448:'1年/次',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08105#1HHHHH704BF1749EE0530E0AA8C08013'},
        {device:'绝缘装置',parameter:'内侧管地电位',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'1年/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08106#1HHHHH704BF1749EE0530E0AA8C08014'},
        {device:'绝缘装置',parameter:'外侧管地电位',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'1年/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08106#1HHHHH704BF1749EE0530E0AA8C08015'},
        {device:'防浪涌保护器',parameter:'外观检测',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'1年/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08107#1HHHHH704BF1749EE0530E0AA8C08016'},
        {device:'防浪涌保护器',parameter:'站内侧管/地电位',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'1年/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08107#1HHHHH704BF1749EE0530E0AA8C08017'},
        {device:'防浪涌保护器',parameter:'站外侧管/地电位',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'1年/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08107#1HHHHH704BF1749EE0530E0AA8C08018'},
        {device:'牺牲阳极',parameter:'阳极输出电流',gbt19285:'',gbt21448:'1年/次',syt5919:'6月/次',cjj95:'6月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08108#1HHHHH704BF1749EE0530E0AA8C08019'},
        {device:'牺牲阳极',parameter:'牺牲阳极开路电位',gbt19285:'',gbt21448:'1年/次',syt5919:'6月/次',cjj95:'6月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08108#1HHHHH704BF1749EE0530E0AA8C08020'},
        {device:'牺牲阳极',parameter:'通电电位',gbt19285:'6月/次',gbt21448:'1年/次',syt5919:'6月/次',cjj95:'6月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08108#1HHHHH704BF1749EE0530E0AA8C08010'},
        {device:'牺牲阳极',parameter:'接地电阻',gbt19285:'',gbt21448:'',syt5919:'6月/次',cjj95:'6月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08108#1HHHHH704BF1749EE0530E0AA8C08021'},
        {device:'牺牲阳极',parameter:'土壤电阻率',gbt19285:'',gbt21448:'',syt5919:'6月/次',cjj95:'6月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08108#1HHHHH704BF1749EE0530E0AA8C08022'},
        {device:'牺牲阳极',parameter:'牺牲阳极维护',gbt19285:'',gbt21448:'',syt5919:'1年/次',cjj95:'6月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08108#1HHHHH704BF1749EE0530E0AA8C08023'},
        {device:'排流装置',parameter:'平均排流电流',gbt19285:'1月/次',gbt21448:'',syt5919:'1月/次',cjj95:'1月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08024'},
        {device:'排流装置',parameter:'最大排流电流',gbt19285:'1月/次',gbt21448:'',syt5919:'',cjj95:'1月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08025'},
        {device:'排流装置',parameter:'最小排流电流',gbt19285:'1月/次',gbt21448:'',syt5919:'',cjj95:'1月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08026'},
        {device:'排流装置',parameter:'交流电压',gbt19285:'',gbt21448:'',syt5919:'1月/次',cjj95:'1月/次',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08027'},
        {device:'排流装置',parameter:'管地电位',gbt19285:'1月/次',gbt21448:'1年/次',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08028'},
        {device:'排流装置',parameter:'排流地床开路电位',gbt19285:'1月/次',gbt21448:'1年/次',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08029'},
        {device:'排流装置',parameter:'排流地床接地电阻',gbt19285:'1月/次',gbt21448:'1年/次',syt5919:'',cjj95:'',custom:'',cycle:'',id:'1HHHHH704BF1749EE0530E0AA8C08109#1HHHHH704BF1749EE0530E0AA8C08030'}
    ];

    // 返回枚举 - 1
    function cycle(d){
        switch (d){
            case '1天/次':
                return 'EVERY_DAY';
            break;
            case '1周/次':
                return 'WEEKLY';
                break;
            case '2周/次':
                return 'TWO_WEEK';
                break;
            case '1月/次':
                return 'MONTHLY';
                break;
            case '2月/次':
                return 'TWO_MONTH';
                break;
            case '3月/次':
                return 'THREE_MONTH';
                break;
            case '6月/次':
                return 'SIX_MONTH';
                break;
            case '1年/次':
                return 'ANNUALLY';
                break;
            default:
                return '';
            break;
        }
    }

    // 返回枚举 - 2
    function cycle2(d){
        switch (d){
            case 'EVERY_DAY':
                return '1天/次';
                break;
            case 'WEEKLY':
                return '1周/次';
                break;
            case 'TWO_WEEK':
                return '2周/次';
                break;
            case 'MONTHLY':
                return '1月/次';
                break;
            case 'TWO_MONTH':
                return '2月/次';
                break;
            case 'THREE_MONTH':
                return '3月/次';
                break;
            case 'SIX_MONTH':
                return '6月/次';
                break;
            case 'ANNUALLY':
                return '1年/次';
                break;
            default:
                return '';
                break;
        }
    }

    // 填充标准列表
    function fillPlan(){
        var td = '', tr = '';
        for(var i = 0, len = planList.length; i < len; i++){
            var ids = (planList[i].id).split('#');

            var gbt19285 = function(){
                return cycle(planList[i].gbt19285);
            }();
            var gbt21448 = function(){
                return cycle(planList[i].gbt21448);
            }();
            var syt5919 = function(){
                return cycle(planList[i].syt5919);
            }();
            var cjj95 = function(){
                return cycle(planList[i].cjj95);
            }();

            td = '<td data-value="'+ids[0]+'" title="'+planList[i].device+'">'+planList[i].device+'</td>'
                + '<td data-value="'+ids[1]+'" title="'+planList[i].parameter+'">'+planList[i].parameter+'</td>'
                + '<td data-value="'+gbt19285+'" title="'+planList[i].gbt19285+'">'+planList[i].gbt19285+'</td>'
                + '<td data-value="'+gbt21448+'" title="'+planList[i].gbt21448+'">'+planList[i].gbt21448+'</td>'
                + '<td data-value="'+syt5919+'" title="'+planList[i].syt5919+'">'+planList[i].syt5919+'</td>'
                + '<td data-value="'+cjj95+'" title="'+planList[i].cjj95+'">'+planList[i].cjj95+'</td>'
                + '<td data-value="CUSTOM"><span data-id="'+planList[i].id+'" class="zy-cathodic-custom" data-value=""><input disabled="disabled" type="text" class="zy-cathodic-disabled"><em>无</em><i class="zy-pick-down-icon"></i></span></td>'
                + '<td data-value="cycle"><abbr data-id="'+planList[i].id+'" data-sort="'+i+'" class="zy-cathodic-cycle"></abbr></td>'
                + '<td data-value="date"><i data-id="'+planList[i].id+'" data-sort="'+i+'" class="zy-cathodic-date"></i></td>';
            tr += '<tr id="'+planList[i].id+'">'+td+'</tr>';
            td = '';
        }
        $plan_tbody.empty().html(tr);
    }
    fillPlan();


    // 弹出自定义下拉
    var $pick = $('#JQ_pick_plan');
    $plan_tbody.on('click','span.zy-cathodic-custom',function(){
        var $this = $(this);
        var id = $this.attr('data-id');

        var p_l = $this.position().left + 31;
        var p_t = $this.offset().top;
        var p_w = $this.width();

        $pick.css({
            'width': p_w,
            'left': p_l,
            'top': p_t
        }).removeClass('hidden').attr('data-id',id);
    });
    $pick.on('mouseleave',function(){
        $(this).addClass('hidden');
    });
    $pick.on('click','li',function(event){
        var $this = $(this);
        var value = $this.attr('data-value');
        var text = $this.text();

        var id = $pick.attr('data-id');
        var $pick_span = $plan_tbody.find('span[data-id="'+id+'"]');
        $pick_span.attr('data-value',value);
        $pick_span.find('em').text(text);

        $pick.addClass('hidden');

        event.stopPropagation();
        return false;
    });

    // 条件
    var filter = {
        getSearch: function(){
            var data = {};
            data.mode = true;
            data.pipeId = function(){
                var v = $pipeline.val();
                if(v){
                    return v;
                }else{
                    $pipeline.element.TIP({content: '管道名称不能为空!'});
                    data.mode = false;
                }
            }();
            data.begin = function(){
                var v = $begin.val();
                if(v){
                    return v;
                }else{
                    $begin.element.TIP({content: '起点位置不能为空!'});
                    data.mode = false;
                }
            }();
            data.end = function(){
                var v = $end.val();
                if(v){
                    return v;
                }else{
                    $end.element.TIP({content: '终点位置不能为空!'});
                    data.mode = false;
                }
            }();
            return data;
        }
    };

    // 弹出设置 - 进行单行设置 - 保存
    $plan_tbody.on('click','i.zy-cathodic-date',function(){
        $('#JQ_plan_form').reset();
        $day_week_month_select.fit('DAY');

        var $this = $(this);

        var param = filter.getSearch();

        if(param.mode){
            var sort = $this.attr('data-sort');
            var id = $this.attr('data-id');

            // 如果是编辑,获取已有数据并填充 -------------- start
            var store = $plan_tbody.find('abbr[data-id="'+id+'"]').data('store');
            if(store){
                var index = store.index;

                var s_beginPlanDate = Dyy.getDate(store['proParams['+index+'].beginPlanDate']);
                if(s_beginPlanDate && s_beginPlanDate !== '-'){
                    $date_begin.val(s_beginPlanDate);
                }

                $('#JQ_never_icon').removeClass('active');

                $('#JQ_count_icon').removeClass('active');
                $('#JQ_count_input').attr('disabled','disabled');

                $('#JQ_date_end_icon').removeClass('active');
                $('#JQ_date_end_input').attr('disabled','disabled');

                var s_endMode = store['proParams['+index+'].endMode'];
                switch (s_endMode){
                    case 'FOREVER':
                        $('#JQ_never_icon').addClass('active');
                        break;
                    case 'COUNT':
                        $('#JQ_count_icon').addClass('active');
                        var s_endCount = store['proParams['+index+'].endCount'];
                        $('#JQ_count_input').val(s_endCount).removeAttr('disabled');
                        break;
                    case 'DATE':
                        $('#JQ_date_end_icon').addClass('active');
                        var s_endDate = store['proParams['+index+'].endDate'];
                        $('#JQ_date_end_input').val(Dyy.getDate(s_endDate)).removeAttr('disabled');
                        break;
                    default:
                        $('#JQ_never_icon').addClass('active');
                        break;
                }
                var s_alertAdvanceCount = store['proParams['+index+'].alertAdvanceCount'];
                if(s_alertAdvanceCount){
                    $('#JQ_day_week_month_input').val(s_alertAdvanceCount);
                }

                var s_alertMode = store['proParams['+index+'].alertMode'];
                if(s_alertMode){
                    $day_week_month_select.fit(s_alertMode);
                }

                var s_alertAdvanceTime = store['proParams['+index+'].alertAdvanceTime'];
                if(s_alertAdvanceTime){
                    $('#JQ_pick_time').val(s_alertAdvanceTime);
                }
            }
            // 如果是编辑,获取已有数据并填充 -------------- end

            var ids = id.split('#');
            var wind = $this.WIND({
                width: 640,
                height: 420,
                title: '设置',
                actionBox: 'div.plan-box',
                closeBtn: 'i.plan-close',
                yesBtn: 'JQ_action_yes_btn',
                noBtn: 'JQ_action_no_btn',
                yesBtnTxt: '立即保存',
                noBtnTxt: '确认',
                yesBtnAct: function(){

                    /*** 保存单条数据 - start ***/
                    var $never = $('#JQ_never_icon');
                    var $count_icon = $('#JQ_count_icon');
                    var $count_input = $('#JQ_count_input');
                    var $end_icon = $('#JQ_date_end_icon');
                    var $day_week_month_input = $('#JQ_day_week_month_input');
                    var $pick_time = $('#JQ_pick_time');

                    var data = {};
                    data.mode = true;

                    // 管道数据
                    data.pipeLineId = function(){
                        return $pipeline.val();
                    }();
                    data.fromPointId = function(){
                        return $begin.val();
                    }();
                    data.fromPointMileage = function(){
                        return $begin.val('mile');
                    }();
                    data.toPointId = function(){
                        return $end.val();
                    }();
                    data.toPointMileage = function(){
                        return $end.val('mile');
                    }();
                    data.use = function(){
                        var use = '';
                        var $radio = $radio_group.find('i.zy-radio-icon');
                        if($radio.length > 0){
                            $radio.each(function(){
                                var $this = $(this);
                                if($this.hasClass('active')){
                                    use = $this.attr('data-id');
                                }
                            });
                        }
                        return use;
                    }();
                    data.dataStated = function(){
                        return '03';
                    }();
                    data.applyTo = function(){
                        return 'PIPELINE';
                    }();

                    // 设置数据
                    data['proParams[0].deviceObject'] = function(){
                        return ids[0];
                    }();
                    data['proParams[0].deviceParam'] = function(){
                        return ids[1];
                    }();
                    data['proParams[0].frequency'] = function(){
                        var $span = $plan_tbody.find('span[data-id="'+id+'"]');
                        return $span.attr('data-value');
                    }();
                    data['proParams[0].beginPlanDate'] = function(){
                        var v = $.trim($date_begin.val());
                        if(v){
                            return v;
                        }else{
                            $date_begin.TIP({content: '开始时间不能为空!'});
                            data.mode = false;
                        }
                        return '';
                    }();
                    data['proParams[0].endMode'] = function(){
                        if($never.hasClass('active')){
                            return 'FOREVER';
                        }else if($count_icon.hasClass('active')){
                            return 'COUNT';
                        }else if($end_icon.hasClass('active')){
                            return 'DATE';
                        }
                        return '';
                    }();
                    data['proParams[0].endCount'] = function(){
                        if($count_icon.hasClass('active')){
                            var v = $.trim($count_input.val());
                            if(v){
                                if(isNaN(v)){
                                    $count_input.TIP({content: '发生次数须为数字!'});
                                    data.mode = false;
                                }else{
                                    if(v <= 0){
                                        $count_input.TIP({content: '发生次数须大于0!'});
                                        data.mode = false;
                                    }else{
                                        var temp = v.indexOf('.');
                                        if(temp != -1){
                                            $count_input.TIP({content: '发生次数须为正整数!'});
                                            data.mode = false;
                                        }
                                    }
                                    return v;
                                }
                            }else{
                                $count_input.TIP({content: '发生次数不能为空!'});
                                data.mode = false;
                            }
                            return '';
                        }
                        return '';
                    }();
                    data['proParams[0].endDate'] = function(){
                        if($end_icon.hasClass('active')){
                            var v1 = $.trim($date_begin.val());
                            var v2 = $.trim($date_end_input.val());
                            if(v2){
                                if(!(Dyy.checkDateLate(v1,v2))){
                                    $date_end_input.TIP({content:'结束时间不能早于开始时间!'});
                                    data.mode = false;
                                }
                                return v2;
                            }else{
                                $date_end_input.TIP({content:'结束时间不能为空!'});
                                data.mode = false;
                            }
                        }
                        return '';
                    }();
                    data['proParams[0].alertMode'] = function(){
                        return $day_week_month_select.val();
                    }();
                    data['proParams[0].alertAdvanceCount'] = function(){
                        var v = $.trim($day_week_month_input.val());
                        if(v){
                            if(isNaN(v)){
                                $day_week_month_input.TIP({content: '提醒时间须为数字!'});
                                data.mode = false;
                            }else{
                                if(v <= 0){
                                    $day_week_month_input.TIP({content: '提醒时间须大于0!'});
                                    data.mode = false;
                                }else{
                                    var temp = v.indexOf('.');
                                    if(temp != -1){
                                        $day_week_month_input.TIP({content: '提醒时间须为正整数!'});
                                        data.mode = false;
                                    }
                                }
                                return v;
                            }
                        }else{
                            $day_week_month_input.TIP({content: '提醒时间不能为空!'});
                            data.mode = false;
                        }
                        return '';
                    }();
                    data['proParams[0].alertAdvanceTime'] = function(){
                        var v = $.trim($pick_time.val());
                        if(v){
                            return v;
                        }else{
                            $pick_time.TIP({content: '结束时间不能为空!'});
                            data.mode = false;
                        }
                        return '';
                    }();
                    /*** 保存单条数据 - end ***/

                    var temp = {};
                    temp['proParams['+sort+'].beginPlanDate'] = data['proParams[0].beginPlanDate'];
                    temp['proParams['+sort+'].endMode'] = data['proParams[0].endMode'];
                    temp['proParams['+sort+'].endCount'] = data['proParams[0].endCount'];
                    temp['proParams['+sort+'].endDate'] = data['proParams[0].endDate'];
                    temp['proParams['+sort+'].alertMode'] = data['proParams[0].alertMode'];
                    temp['proParams['+sort+'].alertAdvanceCount'] = data['proParams[0].alertAdvanceCount'];
                    temp['proParams['+sort+'].alertAdvanceTime'] = data['proParams[0].alertAdvanceTime'];
                    temp['index'] = sort;

                    // 保存操作
                    if(data.mode){
                        DyyBase.ajax(corrctrlApi.url_plan_save_one(), data, function(dataset){
                            if(dataset.bool){
                                wind.clear();
                                $this.MSG({
                                    type: 'success',
                                    content: '保存设置成功!'
                                });

                                // 设置提醒显示
                                var str = dataset.result.result;
                                $plan_tbody.find('abbr[data-id="'+id+'"]').data('store',temp).attr('title',str).html(str);

                            }else{
                                wind.clear();
                                $this.MSG({
                                    type: 'error',
                                    content: dataset.result
                                });
                            }
                        });
                    }
                },
                noBtnAct: function(callback){
                    var $never = $('#JQ_never_icon');
                    var $count_icon = $('#JQ_count_icon');
                    var $count_input = $('#JQ_count_input');
                    var $end_icon = $('#JQ_date_end_icon');
                    var $day_week_month_input = $('#JQ_day_week_month_input');
                    var $pick_time = $('#JQ_pick_time');

                    var data = {};
                    data.mode = true;

                    data['beginPlanDate'] = function(){
                        var v = $.trim($date_begin.val());
                        if(v){
                            return v;
                        }else{
                            $date_begin.TIP({content: '开始时间不能为空!'});
                            data.mode = false;
                        }
                        return '';
                    }();
                    data['endMode'] = function(){
                        if($never.hasClass('active')){
                            return 'FOREVER';
                        }else if($count_icon.hasClass('active')){
                            return 'COUNT';
                        }else if($end_icon.hasClass('active')){
                            return 'DATE';
                        }
                        return '';
                    }();
                    data['endCount'] = function(){
                        if($count_icon.hasClass('active')){
                            var v = $.trim($count_input.val());
                            if(v){
                                if(isNaN(v)){
                                    $count_input.TIP({content: '发生次数须为数字!'});
                                    data.mode = false;
                                }else{
                                    if(v <= 0){
                                        $count_input.TIP({content: '发生次数须大于0!'});
                                        data.mode = false;
                                    }else{
                                        var temp = v.indexOf('.');
                                        if(temp != -1){
                                            $count_input.TIP({content: '发生次数须为正整数!'});
                                            data.mode = false;
                                        }
                                    }
                                    return v;
                                }
                            }else{
                                $count_input.TIP({content: '发生次数不能为空!'});
                                data.mode = false;
                            }
                            return '';
                        }
                        return '';
                    }();
                    data['endDate'] = function(){
                        if($end_icon.hasClass('active')){
                            var v1 = $.trim($date_begin.val());
                            var v2 = $.trim($date_end_input.val());
                            if(v2){
                                if(!(Dyy.checkDateLate(v1,v2))){
                                    $date_end_input.TIP({content:'结束时间不能早于开始时间!'});
                                    data.mode = false;
                                }
                                return v2;
                            }else{
                                $date_end_input.TIP({content:'结束时间不能为空!'});
                                data.mode = false;
                            }
                        }
                        return '';
                    }();
                    data['alertMode'] = function(){
                        return $day_week_month_select.val();
                    }();
                    data['alertAdvanceCount'] = function(){
                        var v = $.trim($day_week_month_input.val());
                        if(v){
                            if(isNaN(v)){
                                $day_week_month_input.TIP({content: '提醒时间须为数字!'});
                                data.mode = false;
                            }else{
                                if(v <= 0){
                                    $day_week_month_input.TIP({content: '提醒时间须大于0!'});
                                    data.mode = false;
                                }else{
                                    var temp = v.indexOf('.');
                                    if(temp != -1){
                                        $day_week_month_input.TIP({content: '提醒时间须为正整数!'});
                                        data.mode = false;
                                    }
                                }
                                return v;
                            }
                        }else{
                            $day_week_month_input.TIP({content: '提醒时间不能为空!'});
                            data.mode = false;
                        }
                        return '';
                    }();
                    data['alertAdvanceTime'] = function(){
                        var v = $.trim($pick_time.val());
                        if(v){
                            return v;
                        }else{
                            $pick_time.TIP({content: '结束时间不能为空!'});
                            data.mode = false;
                        }
                        return '';
                    }();

                    var temp = {};
                    temp['proParams['+sort+'].beginPlanDate'] = data['beginPlanDate'];
                    temp['proParams['+sort+'].endMode'] = data['endMode'];
                    temp['proParams['+sort+'].endCount'] = data['endCount'];
                    temp['proParams['+sort+'].endDate'] = data['endDate'];
                    temp['proParams['+sort+'].alertMode'] = data['alertMode'];
                    temp['proParams['+sort+'].alertAdvanceCount'] = data['alertAdvanceCount'];
                    temp['proParams['+sort+'].alertAdvanceTime'] = data['alertAdvanceTime'];
                    temp['index'] = sort;

                    if(data.mode){
                        DyyBase.ajax(corrctrlApi.url_plan_save_param(), data, function(dataset){
                            if(dataset.bool){
                                // 设置提醒显示
                                var str = dataset.result.result;
                                $plan_tbody.find('abbr[data-id="'+id+'"]').data('store',temp).attr('title',str).html(str);
                                callback();
                            }
                        });
                    }
                }
            });
        }
    });


    // 增减时间
    var $pick_time = $('#JQ_pick_time');
    function checkTime(){
        var time = {};
        time.mode = true;
        var t = $pick_time.val();
        t = t.split(':');
        if(t.length == 2){
            if(isNaN(t[0])){
                time.mode = false;
                $pick_time.TIP({content:'请输入正确的小时时间!'});
            }else{
                if(t[0] > 23){
                    time.mode = false;
                    $pick_time.TIP({content:'小时不超过23!'});
                }else{
                    time.hour = t[0];
                }
            }
            if(isNaN(t[1])){
                time.mode = false;
                $pick_time.TIP({content:'请输入正确的分钟时间!'});
            }else{
                if(t[1] > 59){
                    time.mode = false;
                    $pick_time.TIP({content:'分钟不超过59!'});
                }else{
                    time.min = t[1];
                }
            }
        }else{
            time.mode = false;
            $pick_time.TIP({content:'请输入正确时间格式!'});
        }
        return time;
    }
    $pick_time.on('blur',function(){
        checkTime();
    });
    $('#JQ_time_h_up').click(function(){
        var b = checkTime();
        if(b.mode){
            var hour = parseInt(b.hour) + 1;
            if(hour > 23){
                hour = 0;
            }
            hour = hour.toString();
            hour = hour.length == 1 ? '0' + hour : hour;
            var time = hour + ':' + b.min;
            $pick_time.val(time);
        }
    });
    $('#JQ_time_h_down').click(function(){
        var b = checkTime();
        if(b.mode){
            var hour = parseInt(b.hour) - 1;
            if(hour < 0){
                hour = 23;
            }
            hour = hour.toString();
            hour = hour.length == 1 ? '0' + hour : hour;
            var time = hour + ':' + b.min;
            $pick_time.val(time);
        }
    });
    $('#JQ_time_m_up').click(function(){
        var b = checkTime();
        if(b.mode){
            var min = parseInt(b.min) + 1;
            if(min > 59){
                min = 0;
            }
            min = min.toString();
            min = min.length == 1 ? '0' + min : min;
            var time = b.hour + ':' + min;
            $pick_time.val(time);
        }
    });
    $('#JQ_time_m_down').click(function(){
        var b = checkTime();
        if(b.mode){
            var min = parseInt(b.min) - 1;
            if(min < 0){
                min = 0;
            }
            min = min.toString();
            min = min.length == 1 ? '0' + min : min;
            var time = b.hour + ':' + min;
            $pick_time.val(time);
        }
    });

    // 单选触发 - 规范
    var $radio_group = $('#JQ_radio_group');
    $radio_group.on('change','i.zy-radio-icon',function(){
        var $this = $(this);
        var id = $this.attr('data-id');
        if(id == 'CUSTOM'){
            $plan_tbody.find('input.zy-cathodic-disabled').remove();
        }else{
            var $disabled = '<input disabled="disabled" type="text" class="zy-cathodic-disabled">';
            var exist = $plan_tbody.find('span.zy-cathodic-custom').find('input.zy-cathodic-disabled');
            if(exist.length == 0){
                $plan_tbody.find('span.zy-cathodic-custom').find('em').before($disabled);
            }
        }
    });
    // 单选触发 - 设置
    $('#JQ_radio_mode').on('change','i.zy-radio-icon',function(){
        var $this = $(this);
        $('#JQ_radio_mode').find('input.zy-plan-input').attr('disabled','disabled');
        $this.siblings('input.zy-plan-input').removeAttr('disabled');
    });


    /*** 底部操作 - start ***/
    // 保存 & 发布
    function setAction(tag,$this){
        // 传输对象
        var data = {};
        data.mode = true;

        // 管道数据
        data.pipeLineId = function(){
            var v = $pipeline.val();
            if(v){
                return v;
            }else{
                $pipeline.element.TIP({content: '管道名称不能为空!'});
                data.mode = false;
            }
            return '';
        }();
        data.fromPointId = function(){
            var v = $begin.val();
            if(v){
                return v;
            }else{
                $begin.element.TIP({content: '起点位置不能为空!'});
                data.mode = false;
            }
            return '';
        }();
        data.fromPointMileage = function(){
            return $begin.val('mile');
        }();
        data.toPointId = function(){
            var v = $end.val();
            if(v){
                return v;
            }else{
                $end.element.TIP({content: '终点位置不能为空!'});
                data.mode = false;
            }
            return '';
        }();
        data.toPointMileage = function(){
            return $end.val('mile');
        }();
        data.use = function(){
            var use = '';
            var $radio = $radio_group.find('i.zy-radio-icon');
            if($radio.length > 0){
                $radio.each(function(){
                    var $this = $(this);
                    if($this.hasClass('active')){
                        use = $this.attr('data-id');
                    }
                });
            }
            return use;
        }();
        data.dataStated = function(){
            return tag;
        }();
        data.applyTo = function(){
            return 'PIPELINE';
        }();

        // 填充数据
        var $tr = $plan_tbody.find('tr');
        if($tr.length > 0){
            $tr.each(function(i){
                var $this = $(this);
                var $tds = $this.find('td');
                var id = $this.attr('id');

                data['proParams['+i+'].frequency'] = function(){
                    switch (data.use){
                        case 'GB_T_19285_2014':
                            return $tds.eq(2).attr('data-value');
                            break;
                        case 'GB_T_21448_2008':
                            return $tds.eq(3).attr('data-value');
                            break;
                        case 'SY_T_5919_2009':
                            return $tds.eq(4).attr('data-value');
                            break;
                        case 'CJJ_95_2013':
                            return $tds.eq(5).attr('data-value');
                            break;
                        case 'CUSTOM':
                            return $tds.eq(6).find('span').attr('data-value');
                            break;
                        default:
                            return '';
                            break;
                    }
                }();

                data['proParams['+i+'].deviceObject'] = function(){
                    return $tds.eq(0).attr('data-value');
                }();

                data['proParams['+i+'].deviceParam'] = function(){
                    return $tds.eq(1).attr('data-value');
                }();

                var abbr = $plan_tbody.find('abbr[data-id="'+id+'"]');
                var abbr_data = abbr.data('store');
                if(abbr_data){
                    data['proParams['+i+'].beginPlanDate'] = abbr_data['proParams['+i+'].beginPlanDate'];
                    data['proParams['+i+'].endMode'] = abbr_data['proParams['+i+'].endMode'];
                    data['proParams['+i+'].endDate'] = abbr_data['proParams['+i+'].endDate'];
                    data['proParams['+i+'].endCount'] = abbr_data['proParams['+i+'].endCount'];
                    data['proParams['+i+'].alertMode'] = abbr_data['proParams['+i+'].alertMode'];
                    data['proParams['+i+'].alertAdvanceCount'] = abbr_data['proParams['+i+'].alertAdvanceCount'];
                    data['proParams['+i+'].alertAdvanceTime'] = abbr_data['proParams['+i+'].alertAdvanceTime'];
                }

            });
        }

        // 保存操作
        if(data.mode){
            // 判断是否交叉
            var check = false;

            DyyBase.ajax(corrctrlApi.url_plan_check_cross(), data, function(dataset){
                if(dataset.bool){
                    check = true;
                }
            },{async:false});

            if(check){
                DyyBase.ajax(corrctrlApi.url_plan_save_all(), data, function(dataset){
                    if(dataset.bool){
                        $this.MSG({
                            type: 'success',
                            content: dataset.result,
                            yesBtnAct: DyyBase.goToPage,
                            closeBtnAct: DyyBase.goToPage,
                            goToUrl: DyyBase.root_path + '/corrctrl/operationPlan/toPlanList.html'
                        });
                    }else{
                        $this.MSG({
                            type: 'error',
                            content: dataset.result
                        });
                    }
                });
            }
        }
    }

    // 全部保存
    $('#JQ_save').click(function(){
        var $this = $(this);
        setAction($this.attr('data-value'),$this);
    });

    // 发布
    $('#JQ_release').click(function(){
        var $this = $(this);
        setAction($this.attr('data-value'),$this);
    });

    // 返回
    $('#JQ_goback').click(function(){
        window.location.href = DyyBase.root_path + '/corrctrl/operationPlan/toPlanList.html';
    });
    /*** 底部操作 - end ***/

    /*** 编辑 ***/
    // 设置单选选中
    function setRadio(tag){
        $radio_group.find('i.zy-radio-icon').removeClass('active').removeClass('disabled');
        switch (tag){
            case 'GB_T_19285_2014':
                $radio_group.find('i[data-id="'+tag+'"]').addClass('active');
                break;
            case 'GB_T_21448_2008':
                $radio_group.find('i[data-id="'+tag+'"]').addClass('active');
                break;
            case 'SY_T_5919_2009':
                $radio_group.find('i[data-id="'+tag+'"]').addClass('active');
                break;
            case 'CJJ_95_2013':
                $radio_group.find('i[data-id="'+tag+'"]').addClass('active');
                break;
            case 'CUSTOM':
                $radio_group.find('i[data-id="'+tag+'"]').addClass('active');
                $plan_tbody.find('input.zy-cathodic-disabled').remove();
                break;
            case 'default':
                $radio_group.find('i.zy-radio-icon').eq(0).addClass('active');
                break;
            default:
                break;
        }
    }

    var pid_edit = $('#JQ_pipeLineId').val();
    var fid_edit = $('#JQ_fromPointId').val();
    var fname_edit = $('#JQ_fromName').val();
    var tid_edit = $('#JQ_toPointId').val();
    var tname_edit = $('#JQ_toName').val();
    var applyTo = $('#JQ_applyTo').val();
    var fromMileage2 = $('#JQ_fromMileage2').val();
    var toMileage2 = $('#JQ_toMileage2').val();
    if(pid_edit){
        var fpa = [], fpo = {};
        fpo.id = fid_edit; fpo.text = fname_edit; fpo.mile = fromMileage2; fpa.push(fpo);

        var tpa = [], tpo = {};
        tpo.id = tid_edit; tpo.text = tname_edit; tpo.mile = toMileage2; tpa.push(tpo);

        //
        $pipeline.fit(pid_edit).disable();
        //
        $segment.disable();
        //
        $begin.asset(fpa).disable();
        //
        $end.asset(tpa).disable();

        DyyBase.ajax(corrctrlApi.url_plan_room_search(),{pipeLineId:pid_edit,fromPointId:fid_edit,toPointId:tid_edit, applyTo: applyTo}, function(dataset){
            if(dataset.bool){
                //
                setRadio(dataset.result.use);
                for(var i = 0, len = dataset.result.proParams.length; i < len; i++){
                    var id = function(){
                        var i1 = dataset.result.proParams[i].deviceObject;
                        var i2 = dataset.result.proParams[i].deviceParam;
                        return i1 + '#' + i2;
                    }();
                    var result = dataset.result.proParams[i].result;
                    var cycle = cycle2(dataset.result.proParams[i].frequency);
                    var $span = $plan_tbody.find('span[data-id="'+id+'"]');
                    var $abbr = $plan_tbody.find('abbr[data-id="'+id+'"]');

                    $span.attr('data-value',dataset.result.proParams[i].frequency).find('em').html(cycle);

                    var sort = $abbr.attr('data-sort');
                    var temp = {};
                    temp['proParams['+sort+'].beginPlanDate'] = dataset.result.proParams[i].beginPlanDate;
                    temp['proParams['+sort+'].endMode'] = dataset.result.proParams[i].endMode;
                    temp['proParams['+sort+'].endDate'] = dataset.result.proParams[i].endDate;
                    temp['proParams['+sort+'].endCount'] = dataset.result.proParams[i].endCount;
                    temp['proParams['+sort+'].alertMode'] = dataset.result.proParams[i].alertMode;
                    temp['proParams['+sort+'].alertAdvanceCount'] = dataset.result.proParams[i].alertAdvanceCount;
                    temp['proParams['+sort+'].alertAdvanceTime'] = dataset.result.proParams[i].alertAdvanceTime;
                    temp['index'] = sort;

                    $abbr.data('store',temp).attr('title',result).html(result);
                }
            }
        });
    }

})(jQuery,window,document);